package com.example.smartplay.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.smartplay.entity.Apply;
import com.example.smartplay.entity.Comments;
import com.example.smartplay.entity.Notice;
import com.example.smartplay.entity.Result;
import com.example.smartplay.mapper.CommentsMapper;
import com.example.smartplay.service.CommentsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.smartplay.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2024-07-11
 */
@Service
public class CommentsServiceImpl extends ServiceImpl<CommentsMapper, Comments> implements CommentsService {
    @Autowired
    private CommentsMapper commentsMapper;


    @Override
    public Result getcomments(Integer pageNum, Integer pageSize) {
        Page<Comments> page = new Page<>(pageNum, pageSize);
        // 查询当前登录用户的 clubId（假设从 JWT 中获取）
        Map<String, Object> map = ThreadLocalUtil.get();

        //查询登录用户的role信息
        String role = (String) map.get("role");
        //查询登录用户的id
        Integer id = (Integer) map.get("id");

        // 构造查询条件
        QueryWrapper<Comments> wrapper = new QueryWrapper<>();
        if (role.equals("ADMIN")) {
            // 管理员角色，不添加任何额外条件，可以查看所有评论

        } else {
            // 普通学生只能查看自己的评论
            wrapper.eq("user_id", id);
        }
        return Result.success(commentsMapper.selectPage(page,wrapper));
    }
}
